package cn.edu.hqu.example.bj.experiment5_3;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteAbortException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends AppCompatActivity {

    SQLiteDatabase db;
    Button bn=null;
    ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建或打开数据库（此处需要使用绝对路径）
        db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/my.db3",null);
        listView =(ListView)findViewById(R.id.show);
        bn=(Button)findViewById(R.id.ok);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入
                String title=((EditText)findViewById(R.id.title)).getText().toString();
                String content=((EditText)findViewById(R.id.content)).getText().toString();

                //先尝试输入如果没有表格就创建新的数据表
                try
                {
                    inserData(db,title,content);
                    //查询news_info表的所有记录
                    Cursor cursor=db.rawQuery("select * from news_info",null);
                    inflateListView(cursor);
                }
                catch (SQLiteException se)
                {
                    //执行ddl创建数据表
                    db.execSQL("create table news_info(_id integer primary key autoincrement,news_title varchar(50),news_content varchar(255)) ");
                    //执行inser语句插入数据
                    inserData(db,title,content);
                    //查询news_info表的所有记录
                    Cursor cursor=db.rawQuery("select * from news_info",null);
                    inflateListView(cursor);
                }
            }
        });
    }

    private void inserData(SQLiteDatabase db,String title,String content)
    {
        db.execSQL("insert into news_info values(null,?,?)",new String[]{
                title,content
        });
    }

    @SuppressLint("NewApi")
    private void inflateListView(Cursor cursor)
    {
        //填充SimpleCursorAdapter,注意控件对应关系
        SimpleCursorAdapter adapter=new SimpleCursorAdapter(
                MainActivity.this,R.layout.line,cursor,
                new String[]{"news_title","news_content"},
                new int[]{R.id.my_title,R.id.my_content},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
                );
        //显示数据
        listView.setAdapter(adapter);
    }

    @Override
    public void  onDestroy()
    {
        super.onDestroy();
        //退出程序时关闭SQLiteDatabase
        if (db!=null && db.isOpen())
        {
            db.close();
        }
    }
}
